$(function() {
    loadStyleTable();
	
	$('#new_style_design_panel').dialog({
        autoOpen: false,
        resizable: false,
        modal: true,
        width: 800,
        title: "新建样式",
   	});
   	
   	$('#prompt_preview_div').dialog({
        autoOpen: false,
        resizable: false,
        modal: true,
        width: 800,
        title: "新建样式预览"
   	});
});


function loadStyleTable() {
  $("#style_list_table").jqGrid({
    url: "styles.action",
    datatype: "json",
    height: 200,
    width: 750,
    colNames: ["编号", "样式编码", "欢迎词", "皮肤", "是否显示天气","",""],
	colModel: [{name: "TEMPLET_STYLE_ID",index: "TEMPLET_STYLE_ID",hidden: true},
			   {name: "TEMPLET_STYLE_ID",index: "TEMPLET_STYLE_ID",align:"center",sortable: false},
			   {name: "WELCOME",index: "WELCOME",align:"center",sortable: false,width: "200"},
			   {name: "SKIN_ID",index: "SKIN_ID",align:"center",sortable: false},
			   {name: "IS_WEATHER_DISPLAY",index: "IS_WEATHER_DISPLAY",align:"center",sortable: false},
			   {name: "TEMPLET_ID",index: "TEMPLET_ID",align:"center",hidden:true},
			   {name: "IS_WEATHER",index: "IS_WEATHER",align:"center",hidden:true}
			  ],
    viewrecords: true,
    multiselect: true,
    rowNum: 5,
    rowList: [5, 10],
    loadComplete: function () {
        setGridCellHeight('style_list_table',30);
        //freshPreview();
    },
    onSelectRow: function(rowid, status) {
    },
    jsonReader: {
      root: "dataset",
      page: "pager.curPageNo",
      total: "pager.totalPages",
      records: "pager.totalCount",
      repeatitems: false
    },
    prmNames: {
      rows: "pager.pageSize",
      page: "pager.curPageNo",
      sort: "pager.orderBy",
      order: "pager.order"
    },
    pager: "#style_list_pager",
    caption: "管理已有样式"
  });
}

function formateStyleDelLink(cellvalue, options, rowObject) {
   var title = "<span style='text-decoration:underline;font-size:13px;color:blue;'>删除</span>"
   return "<a href='javascript:deleteStyleSubByLink("+rowObject.TEMPLET_STYLE_ID+");'>" + title + "</a>";
}

function deleteStylesSubByButton() {
	var rowids=$("#style_list_table").jqGrid('getGridParam','selarrrow');
	if(rowids==''){
		alert("请在下表中选择要删除的样式");
	  	return;
	}
	
	rowidArray = (''+rowids).split(",");
	
	var style_ids='';
	for(var i = 0;i<rowidArray.length;i++){
	  var row_data = $("#style_list_table").jqGrid("getRowData", rowidArray[i]);
	  style_ids += row_data.TEMPLET_STYLE_ID+',';
	}
	style_ids = style_ids.slice(0,style_ids.length-1);
	deleteStylesSub(style_ids);
}


function deleteStyleSubByLink(rule_id){
	deleteStylesSub(rule_id);
}

function deleteStylesSub(style_id){
  var params = {styleIds:style_id}
  
  $.post("delstyle.action",params,function(data) {
  	if(validate_response_result(data.result)){
    	$("#style_list_table").trigger("reloadGrid");
  	}
  },"json");
}

function setStyleTableSelection() {
  var records = $("#style_list_table").jqGrid("getGridParam", "records");
  if(records==null){
    return;
  }
  if(records > 0){
    $("#style_list_table").setSelection(1);
  }else{
  }
}

function init_templet() {
	if(empty(buildItemJsonStr())) {
		alert("请先选择要推广的宝贝！！！");
		$('.tab1').click();
		return;
	}
	var init_v = $("#init_indicator").val();
	if(init_v=="init") display_templet("templet_1");
	else $('#new_style_design_panel').dialog("open");
}

function display_templet(templetName) {
	$('#new_style_design_panel').dialog("open");
}

function preview(){
	$('#choose_style_preview').html("");
	var params = getStyle();
	var rule_type = $("input[name='rule_type']").val();
	if("TF_M_USER_RULE_RANDOM"!==rule_type){
		var itemStr = buildItemJsonStr();
		if(empty(itemStr)) {
			$('#prompt_preview_div').html("<div class='red fb f22'>请在第1步选择您要推广的宝贝</div>");
			$('#prompt_preview_div').dialog("open");
		    return;
	    }
	    params["itemsStr"] = itemStr;
	}
    params["ruleType"] = rule_type;
    var html = $.ajax({url:"preview.action",data:params,type:"post",async:false}).responseText;
    
	$('#prompt_preview_div').html(html);
    $('#prompt_preview_div').dialog("open");
}

function svstyle(){
    var param = getStyle();
    
    $.post("svstyle.action", param,
		function(data,status){
			if(Number(data.result.code)<0){
			    alert(data.result.msg);
			}else{
				alert("样式保存成功！！！");
				$('#new_style_design_panel').dialog("close");
			  	$("#style_list_table").trigger("reloadGrid");
			}
		}, 
		"json"
	);
}

function cancel(){
	$('#prompt_preview_div').html("");
	$('#new_style_design_panel').dialog("close");
}

function getStyle(){
    var params = {
     "userTemplet.TEMPLET_ID":$("input[name='t']").val(),
     "userTemplet.SKIN_ID":$("input[name='skin_id']:checked").val(),
     "userTemplet.WELCOME":$("input[name='welcome']").val(),
     "userTemplet.IS_WEATHER":$('input[name=is_weather]:checked').val(),
     "userTemplet.title":$('input[name=title]').val(),
     "userTemplet.numPerTr":$("input[name='num_per_tr']:checked").val(),
     "title":$('input[name=title]').val(),
     "numPerTr":$("input[name='num_per_tr']:checked").val()
    };
    
 	return params;
}

function getChooseStyle(){
	var  rowid = $("#style_list_table").getGridParam( "selrow" ); 
	if (rowid === null) {
        return {};
    }
    var ret = $("#style_list_table").jqGrid("getRowData", rowid);
    
    var params = {
		"userTemplet.TEMPLET_STYLE_ID":ret.TEMPLET_STYLE_ID,
		"userTemplet.TEMPLET_ID":ret.TEMPLET_ID,
		"userTemplet.WELCOME":ret.WELCOME,
		"userTemplet.SKIN_ID":ret.SKIN_ID,
		"userTemplet.IS_WEATHER":ret.IS_WEATHER,
		"title":$('input[name=title]').val(),
     	"numPerTr":$("input[name='num_per_tr']:checked").val()
    };
 	return params;
}

function validate_tab3() {
	var rowid = $("#style_list_table").getGridParam( "selrow" );
	if(empty(rowid)) {
		alert("请先在第三步选择推广样式！！！");
		return false;
	}
	return true;
}